<?php

namespace App\Repository;

use App\Consts\GlobalRelationshipConst;
use App\Models\UserFollowingRelationship;
use Illuminate\Database\Eloquent\Builder;

/**
 * @package App\Repository
 * @see RelationshipRepository
 */
class RelationshipRepository extends BaseRepository
{

    /**
     * @param $searchParams
     * @return Builder
     */
    public function getRelationshipList($searchParams)
    {
        $provider = UserFollowingRelationship::getModel($searchParams['user_id'])
            ->where('user_id', $searchParams['user_id']);
        $provider->whereIn('attention_status', (array)$searchParams['attention_status']);

        if (!empty($searchParams['last_id'])) {
            $id = UserFollowingRelationship::getModel($searchParams['user_id'])
                ->select(['id'])
                ->where('user_id', $searchParams['user_id'])
                ->where('follow_id', $searchParams['last_id'])
                ->value('id');
            $provider->where('id', '<', $id);
        }

        $provider->orderByDesc('updated_at');
        $provider->orderByDesc('id');


        return $this->setPagination($provider);
    }

    /**
     * @param $userId
     * @param $followId
     * @param $attentionStatus
     * @return UserFollowingRelationship|Builder|\Illuminate\Database\Eloquent\Model|int|mixed
     */
    public function saveRelationship($userId, $followId, $attentionStatus)
    {
        $relationship = $this->getRelationshipById($userId, $followId);
        $loadData = [
            'user_id' => $userId,
            'follow_id' => $followId,
            'attention_status' => $attentionStatus,
        ];
        if (!$relationship) {
            return UserFollowingRelationship::getModel($userId)->create($loadData);
        }

        return $relationship->updateFormat($loadData);
    }

    /**
     * @param $userId
     * @return \Illuminate\Support\Collection
     */
    public function getUserAllFollowingsIdArr($userId)
    {
        $model = UserFollowingRelationship::getModel($userId);
        return $model
            ->where(['user_id' => $userId, 'attention_status' => [GlobalRelationshipConst::ATTENTION_STATUS_ATTENTION, GlobalRelationshipConst::ATTENTION_STATUS_EACH]])
            ->groupBy('follow_id')
            ->pluck('follow_id');
    }

    /**
     * @param $userId
     * @return \Illuminate\Support\Collection
     */
    public function getUserAllFollowersIdArr($userId)
    {
        return UserFollowingRelationship::getModel($userId)
            ->where('user_id', $userId)
            ->whereIn('attention_status', [GlobalRelationshipConst::ATTENTION_STATUS_FANS, GlobalRelationshipConst::ATTENTION_STATUS_EACH])
            ->groupBy('follow_id')
            ->pluck('follow_id');
    }

    /**
     * @param $userId
     * @param $followId
     * @param $attentionStatus
     * @return UserFollowingRelationship|Builder|\Illuminate\Database\Eloquent\Model|object|null
     */
    public function getRelationshipByIdAndAttentionStatus($userId, $followId, $attentionStatus)
    {
        $query = UserFollowingRelationship::getModel($userId);
        return $query
            ->where(['user_id' => $userId, 'follow_id' => $followId, 'attention_status' => $attentionStatus])
            ->first();
    }

    /**
     * @param $userId
     * @param $followId
     * @return UserFollowingRelationship|Builder|\Illuminate\Database\Eloquent\Model|object|null
     */
    public function getRelationshipById($userId, $followId)
    {
        return UserFollowingRelationship::getModel($userId)
            ->where(['user_id' => $userId, 'follow_id' => $followId])
            ->first();
    }

    /**
     * @param $userId
     * @param $followIdArr
     * @return UserFollowingRelationship|UserFollowingRelationship[]|Builder|Builder[]|\Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Model[]
     */
    public function getRelationshipByIdArr($userId, $followIdArr)
    {
        return UserFollowingRelationship::getModel($userId)
            ->where('user_id', $userId)
            ->whereIn('follow_id', $followIdArr)
            ->get();
    }
}
